package timer;

import configuration.RobConfiguration;
import enumeration.EnumCourseType;
import http.util.PrintUtil;
import main.robClass;
import mysql.MysqlUtil;
import robber.AbstractRobber;
import robber.IRobber;
import robber.impl.CommonRobber;
import robber.impl.PERobber;
import user.IUser;
import java.util.HashMap;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class saveRobberManager {

    /**
     * globalIdAndCookies - 用来记录所有用户的idAndCookies
     * 根据定时任务备份到每一位用户的数据库之中
     * 根据username 获取 IUser对象 从而方便 序列化cookies到指定的user -> mysqlUtil.updateUser(Iuser user)
     **/
    public static HashMap<String, IRobber> globalIdAndRobberInfo = new HashMap<>();

    public static HashMap<String, IRobber> getGlobalIdAndRobberInfo() {
        return globalIdAndRobberInfo;
    }

    public static void setGlobalIdAndRobberInfo(HashMap<String, IRobber> globalIdAndRobberInfo) {
        saveRobberManager.globalIdAndRobberInfo = globalIdAndRobberInfo;
    }

    static {
        robClass.logger.info("saveRobberManager - 管理Cookie备份已加载");
    }

    /**
     * 开启定时备份robber信息
     * 备份的信息是
     * @variable username - 用户名
     * @variable firstViewState - 异步搜索课程后的viewstate 用来获取must3Params(PE)/commonViewState(Common)
     * @variable must3Params - 最终抢课需要的参数
     **/

    public static void updateUserToGlobalIdAndRobberInfo(IRobber iRobber) {
//        PrintUtil.printTitle("updateUserToGlobalIdAndRobberInfo");
        AbstractRobber abstractRobber = (AbstractRobber) iRobber;
        IUser user = abstractRobber.getUser();
        String username = user.getUsername();
        boolean isFirstAdded = !globalIdAndRobberInfo.containsKey(username);
        if (isFirstAdded) {
            if(abstractRobber instanceof PERobber){
                MysqlUtil.updatePERobber(abstractRobber);
            }
            else if(abstractRobber instanceof CommonRobber){
                MysqlUtil.updateCommonRobber(abstractRobber);
            }
          //  PrintUtil.printTitle(username + "已序列化到Robber数据库之中" + user.toString());
        }
        globalIdAndRobberInfo.put(username, iRobber);
        //PrintUtil.printTitle(username + " 已加入全局备份Robber数据中");
    }


    /**
     *10秒备份一次
     **/
    public static void startBackupRobberInfo() {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
//                robClass.logger.info("globalIdAndRobberInfo:" + globalIdAndRobberInfo);
                PrintUtil.printTitle(globalIdAndRobberInfo.entrySet().size()+"个#人员进行Robber备份");
                if (globalIdAndRobberInfo.isEmpty()) {
                    PrintUtil.printTitle("沒有需要备份的人员");
                    return;
                }
                globalIdAndRobberInfo.entrySet().parallelStream().forEach(
                        entry -> {
                            IRobber iRobber = entry.getValue();
                            if(RobConfiguration.ROB_COURSE_TYPE == EnumCourseType.PE){
                                MysqlUtil.updatePERobber(iRobber);
                            }
                            else if(RobConfiguration.ROB_COURSE_TYPE == EnumCourseType.COMMON){
                                MysqlUtil.updateCommonRobber(iRobber);
                            }
//                            robClass.logger.info("为" + ((AbstractRobber) iRobber).getUser().getUsername() + "进行Robber数据备份");
                        }
                );
            }
        }, 10, 10, TimeUnit.SECONDS);
    }
}